<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

  <script src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js"
    type="application/javascript"></script>
</head>

<body>,
  <div id="app">
    <!-- v-指令名="变量名/表达式" -->
    <img :src="staffPhoto" v-imgerror="'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'" />
    <img :src="staffPhoto" v-imgerror="'https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg'" />
  </div>
</body>
<script>
  // 全局自定义指令
  Vue.directive('imgerror', {
    inserted (el, binding, vnode) {
      console.log('inserted');
      if (!el.src) {
        el.src = binding.value
      }
      el.onerror = function () {
        el.src = binding.value
      }
    },
    update (el, binding, vnode, oldVnode) {
      if (!el.src) {
        el.src = binding.value
      }
    },

  });
  new Vue({
    el: '#app',
    data () {
      return {
        // staffPhoto:'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
        // staffPhoto: 'https://localhost:8080/undefined.jpeg'
        staffPhoto: undefined
      }
    },
    created () {
      // 异步模拟各种情况的改变值
      // 情况1：返回一个失效的图片路径
      this.staffPhoto = 'https://localhost:8080/undefined'
      // 情况2：返回空字符串
      this.staffPhoto = ''
      // 情况3：返回undefied,null
      this.staffPhoto = undefined
      this.staffPhoto = null
    }
  })
</script>

</html>